package cn.dengta.webapp.user.dto;

import java.io.Serializable;
import java.util.*;

import cn.dengta.common.context.Env;
import cn.dengta.common.model.Bean;
import cn.dengta.webapp.user.entity.User;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import org.springframework.cglib.beans.BeanMap;

/**
 * 推荐表
 *
 * @author lizhichao
 * @since 2022/9/21
 */
@Setter
@Getter
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserRecDTO implements Serializable, Bean<Long> {

	/** 索引名 */
	public static final String USER_REC_INDEX = "user_rec_";
	/** 备份索引 */
	public static final String USER_REC_BAK_INDEX = "user_rec_bak_";

	/** 用户ID */
	private Long id;
	/** 性别 */
	private Integer gender;
	/** 权重1 （男：是否被搭讪10次，女：是否在推荐白名单） */
	private Boolean weights1;
	/** 权重2 （男：是否上传头像，女：是否完善资料）- */
	private Boolean weights2;
	/** 是否黑名单 */
	private Boolean blacklist;
	/** 昵称 */
	private String nickname;
	/** 最后活跃时间 */
	private Long lastActive;
	/** 登陆时间 */
	private Long loginTime;
	/** 是否实名认证 */
	private Boolean realNamed;
	/** 是否真人认证 */
	private Boolean avatared;
	/** 注册时间 */
	private Date registerTime;
	/** 推广员ID */
	private Long inviteUserId;
	/** 权重，默认1，值越大显示越靠前，小于0不显示 */
	protected Integer weight;
	/** 注册国家 */
	protected String regCountry;
	/** 注册分包 */
	private Integer wesKit;
	/** 用户层级 */
	private Collection<Integer> levels;

	public Map<String, Object> toMap() {
		return BeanMap.create(this);
	}

	public static String indexName() {
		final String prefix = Env.inLocal() || Env.inDev() ? "ly_" : "dt_";
		return prefix + USER_REC_INDEX + Env.CURRENT.value;
	}

	public static String bakIndexName() {
		return USER_REC_BAK_INDEX + Env.CURRENT.value;
	}

	public static UserRecDTO init(User user, List<Integer> levels, Boolean weights1, Boolean weights2, String regCountry) {
		UserRecDTO ur = new UserRecDTO();
		ur.setId(user.getId());
		ur.setNickname(user.getNickname());
		ur.setGender(user.getGender().value);
		ur.setLastActive(user.getLastActive());
		ur.setRealNamed(user.isRealNamed());
		ur.setAvatared(user.isAvatared());
		ur.setRegisterTime(user.getAddTime());
		ur.setInviteUserId(user.getAgentId());
		ur.setLevels(levels);
		ur.setWeights1(weights1);
		ur.setWeights2(weights2);
		ur.setLoginTime(0L);
		ur.setRegCountry(regCountry);
		ur.setWesKit(user.getWesKit().getValue());
		return ur;
	}

	public static UserRecDTO initForUser(User user) {
		UserRecDTO ur = new UserRecDTO();
		ur.setId(user.getId());
		ur.setNickname(user.getNickname());
		ur.setGender(user.getGender().value);
		ur.setLastActive(user.getLastActive());
		ur.setRealNamed(user.isRealNamed());
		ur.setAvatared(user.isAvatared());
		ur.setRegisterTime(user.getAddTime());
		ur.setLoginTime(user.getLastActive());
		ur.setWesKit(user.getWesKit().getValue());
		/*if (user.getGender() == Gender.FEMALE) {
			ur.setWeights1(Cmp.gt(user.getRecLevel().value, 0));
			ur.setWeights2(UserInfoSupply.calcScore(user.getInfoSupplyFlag()) >= 100);
		}*/
		return ur;
	}

	@Override
	public Long getId() {
		return id;
	}

	@Override
	public void setId(Long id) {
		this.id = id;
	}

	public static final String LEVELS = "levels";
	public static final String LAST_ACTIVE = "lastActive";
	public static final String REGISTER_TIME = "registerTime";
	public static final String NICKNAME = "nickname";
	public static final String NICKNAME_KEYWORD = "nickname.keyword";
	public static final String INVITE_USER_ID = "inviteUserId";
	public static final String ID = "id";
	public static final String AVATARED = "avatared";
	public static final String REAL_NAMED = "realNamed";
	public static final String GENDER = "gender";
	public static final String BLACKLIST = "blacklist";
	public static final String WEIGHTS_1 = "weights1";
	public static final String WEIGHTS_2 = "weights2";
	public static final String WEIGHT = "weight";
	public static final String LOGIN_TIME = "loginTime";
	public static final String WES_KIT = "wesKit";

}
